package cn.cmdr.controller;

import cn.cmdr.pojo.Brand;
import cn.cmdr.pojo.Commodity;
import cn.cmdr.pojo.Journal;
import cn.cmdr.pojo.Staff;
import cn.cmdr.service.BrandService;
import cn.cmdr.service.ComService;
import cn.cmdr.service.JournalService;
import cn.cmdr.vo.BrandVo;
import cn.cmdr.vo.PageVo;
import cn.cmdr.vo.ResultVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.*;


@RestController
@RequestMapping("/sys/brand")
public class BrandController {
    @Resource
    private BrandService brandService;
    @Resource
    private ComService comService;
    @Resource
    private JournalService journalService;

    @PostMapping("/brandList/{pageSize}/{pageNo}")
    public ResultVo getList(@PathVariable Integer pageSize, @PathVariable Integer pageNo,@RequestBody BrandVo brandVo){
        return ResultVo.sucess(brandService.getPageBrandList(pageNo,pageSize,brandVo));
    }
    @PostMapping("/list")
    public ResultVo getbrandList(){
        return ResultVo.sucess(brandService.getlist());
    }
    @PostMapping("/addBrand")
    @RequiresPermissions("brand:add")
    public ResultVo addBrand(@RequestBody Brand brand){
        try {
            brandService.addBrand(brand);
            Journal journal = new Journal();
            journal.setStaffId(((Staff)SecurityUtils.getSubject().getPrincipal()).getId());
            journal.setUpDate(new Date());
            journal.setUpContent("添加品牌:"+brand.getName());
            journal.setMenuId(3);
            journalService.add(journal);
            return ResultVo.sucess("添加成功");
        }catch (Exception e){
            return ResultVo.fail("添加失败",e);
        }
    }
    @PostMapping("/update")
    @RequiresPermissions("brand:update")
    public ResultVo update(@RequestBody Brand brand){
        try {
            brandService.updateBrand(brand);
            Journal journal = new Journal();
            journal.setStaffId(((Staff)SecurityUtils.getSubject().getPrincipal()).getId());
            journal.setUpDate(new Date());
            journal.setUpContent("修改品牌:"+brand.getName());
            journal.setMenuId(3);
            journalService.add(journal);
            return ResultVo.sucess("修改成功");
        }catch (Exception e){
            return ResultVo.fail("修改失败",e);
        }
    }
    @GetMapping("/del")
    @RequiresPermissions("brand:del")
    public ResultVo del(Integer[] id){
        try {
            for(int i=0;i<id.length;i++){
                List<Commodity> list = comService.getcBYyId(id[i]);
                Brand brand = brandService.getBrand(id[i]);
                if(list.size()!=0){
                    return ResultVo.fail(brand.getName()+"下还有商品不能删除");
                }else{
                    brandService.del(id);
                    Journal journal = new Journal();
                    journal.setStaffId(((Staff)SecurityUtils.getSubject().getPrincipal()).getId());
                    journal.setUpDate(new Date());
                    journal.setUpContent("删除品牌:"+brand.getName());
                    journal.setMenuId(3);
                    journalService.add(journal);
                }
            }

            return ResultVo.sucess("删除成功");
        }catch (Exception e){
            return ResultVo.fail("删除失败",e);
        }
    }

    @PostMapping("/upload")
    public ResultVo upload(MultipartFile attach, HttpServletRequest request){
        ServletContext servletContext = request.getServletContext();
        String realPath = servletContext.getRealPath("/img");// 获取项目根路径下的imges
        System.out.println(realPath+"======+++++++++++++++++++++++");
        File floader = new File(realPath);
        if (!floader.exists()){
            floader.mkdir();//如果不存在这个文件夹,就创建上
        }
        String original = attach.getOriginalFilename();//文件原始名字  a.jpg
        System.out.println("原始名字 :"+original);
        int index = original.lastIndexOf(".");
        String suffix = original.substring(index);  //.jpg
        String newFileName = UUID.randomUUID().toString().replace("-","")+suffix;//文件新名字
        File file =new File(realPath,newFileName);
        try {
            attach.transferTo(file);//文件上传
        } catch (IOException e) {
            e.printStackTrace();
        }

        Map<String,Object> map = new HashMap<>();
        map.put("attachPath","img/"+newFileName);//images/232132eadsa32332432.jpg
        return ResultVo.sucess(map);
    }

    @PostMapping("/qx")
    private ResultVo qx(@RequestBody Brand brand, HttpServletRequest request){
        System.out.println(brand.getLogo()+"----------------------------------");
        ServletContext servletContext = request.getServletContext();
        String realPath = servletContext.getRealPath("/");
        File f = new File(realPath+brand.getLogo());
        f.delete();
        return ResultVo.sucess("成功");
    }
    @PostMapping("/brandlist")
    public ResultVo brandList(){
        return ResultVo.sucess(brandService.getlist());
    }


}
